<?php

namespace app\admin\controller;


use app\admin\service\DashboardService;
use think\Request;

class Dashboard extends Backend
{
    /**
     * @var DashboardService
     */
    protected $service;

    public function initialize()
    {
        parent::initialize();
        $this->service = new DashboardService(request()->param('module'), $this->userId);
    }

    public function inspection(Request $request)
    {
        $type = $request->post('type', 'all');
        $date = $request->post('date', null);
        if ($date) {
            $date = strtotime($date);
        }
        $data = $this->service->inspection($type, $date);
        $this->jsonReturn(['code' => 0, 'data' => $data]);
    }

    public function street(Request $request)
    {
        $param = $request->param();
        if (!empty($param['date'])) {
            if (is_array($param['date'])) {
                $param['date'] = array_map('strtotime', $param['date']);
            } else {
                $date = strtotime($param['date']);
                $param['date'] = [$date, $date];
            }
            $param['date'][1] += 86399;
        }
        $data = $this->service->street($param);
        $this->jsonReturn(['code' => 0, 'data' => $data]);
    }

    public function earlyWarning()
    {
        $data = $this->service->earlyWarning();
        $this->jsonReturn(['code' => 0, 'data' => $data]);
    }

    public function map(Request $request)
    {
        $param = $request->param();
        if (!empty($param['date'])) {
            $param['date'] = array_map('strtotime', $param['date']);
        }
        $data = $this->service->map($param);
        $this->jsonReturn(['code' => 0, 'data' => $data]);
    }
}